﻿//******************************************************************************************************
//  PackageFile.cs - Gbtc
//
//  Copyright © 2010, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://www.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  07/09/2010 - Stephen C. Wills
//       Generated original version of source code.
//
//******************************************************************************************************

using System.Xml;

namespace TestBenchApi.Package
{
    /// <summary>
    /// Represents a package file that contains settings for a test
    /// to be run by the test bench.
    /// </summary>
    public class PackageFile
    {

        #region [ Members ]

        // Fields

        private GeneratorGroupEntry m_generatorGroup;
        private ConcentratorEntry m_concentrator;
        private ReceiverEntry m_receiver;

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Creates a new instance of the <see cref="PackageFile"/> class.
        /// </summary>
        public PackageFile()
        {
            m_generatorGroup = new GeneratorGroupEntry();
            m_concentrator = new ConcentratorEntry();
            m_receiver = new ReceiverEntry();
        }

        #endregion

        #region [ Properties ]

        /// <summary>
        /// Gets the generator group entry in the package file.
        /// </summary>
        public GeneratorGroupEntry GeneratorGroup
        {
            get
            {
                return m_generatorGroup;
            }
        }

        /// <summary>
        /// Gets the concentrator entry in the package file.
        /// </summary>
        public ConcentratorEntry Concentrator
        {
            get
            {
                return m_concentrator;
            }
        }

        /// <summary>
        /// Gets the receiver entry in the package file.
        /// </summary>
        public ReceiverEntry Receiver
        {
            get
            {
                return m_receiver;
            }
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// Saves the <see cref="PackageFile"/> object to an XML file.
        /// </summary>
        /// <param name="packageFilePath">The path to the XML file.</param>
        public void Save(string packageFilePath)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateNode(XmlNodeType.Element, "testBenchPackage", string.Empty);
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", string.Empty));
            doc.AppendChild(root);
            
            // Save generators.
            XmlNode generatorGroup = doc.CreateNode(XmlNodeType.Element, "generatorGroup", string.Empty);
            root.AppendChild(generatorGroup);

            foreach (string key in m_generatorGroup.Settings.Keys)
            {
                XmlAttribute attribute = doc.CreateAttribute(key);
                attribute.Value = m_generatorGroup.Settings[key];
                generatorGroup.Attributes.Append(attribute);
            }

            // Save concentrator.
            XmlNode concentrator = doc.CreateNode(XmlNodeType.Element, "concentrator", string.Empty);
            root.AppendChild(concentrator);

            foreach (string key in m_concentrator.Settings.Keys)
            {
                XmlAttribute attribute = doc.CreateAttribute(key);
                attribute.Value = m_concentrator.Settings[key];
                concentrator.Attributes.Append(attribute);
            }

            // Save receiver.
            XmlNode receiver = doc.CreateNode(XmlNodeType.Element, "receiver", string.Empty);
            root.AppendChild(receiver);

            foreach (string key in m_receiver.Settings.Keys)
            {
                XmlAttribute attribute = doc.CreateAttribute(key);
                attribute.Value = m_receiver.Settings[key];
                receiver.Attributes.Append(attribute);
            }

            // Save to file.
            doc.Save(packageFilePath);
        }

        #endregion

        #region [ Static ]

        // Static Methods
        
        /// <summary>
        /// Creates a <see cref="PackageFile"/> object and loads settings from an XML file.
        /// </summary>
        /// <param name="packageFilePath">The path to the XML file.</param>
        /// <returns>The <see cref="PackageFile"/> object loaded from XML.</returns>
        public static PackageFile Load(string packageFilePath)
        {
            PackageFile file = new PackageFile();

            // Load file as XML.
            XmlDocument doc = new XmlDocument();
            doc.Load(packageFilePath);

            // Load generators from XML.
            XmlNode generatorGroup = doc.SelectSingleNode("testBenchPackage/generatorGroup");

            foreach (XmlAttribute attribute in generatorGroup.Attributes)
                file.m_generatorGroup.Settings[attribute.Name] = attribute.Value;

            // Load concentrator from XML.
            XmlNode concentrator = doc.SelectSingleNode("testBenchPackage/concentrator");

            foreach (XmlAttribute attribute in concentrator.Attributes)
                file.m_concentrator.Settings[attribute.Name] = attribute.Value;

            // Load receiver from XML.
            XmlNode receiver = doc.SelectSingleNode("testBenchPackage/receiver");

            foreach (XmlAttribute attribute in receiver.Attributes)
                file.m_receiver.Settings[attribute.Name] = attribute.Value;

            return file;
        }

        #endregion
        
    }
}
